package org.ihelpuoo.weboo.module;

import java.util.Iterator;
import java.util.Map;

import org.ihelpuoo.weboo.bean.ActionBean;
import org.ihelpuoo.weboo.bean.ParamBean;
import org.ihelpuoo.weboo.bean.PropertyBean;
import org.ihelpuoo.weboo.exception.ActionConfigInvalidException;
import org.ihelpuoo.weboo.exception.ModuleNotFoundException;
import org.ihelpuoo.weboo.util.ForwardTarget;

public class ActionConfig
{

	static ActionConfig wrap(ActionBean actionBean, ModuleManager moduleManager)
			throws ActionConfigInvalidException
	{
		if (null == actionBean)
			throw new ActionConfigInvalidException("Action can not be NULL!");

		ActionConfig config = new ActionConfig(actionBean,moduleManager);
		return config;
	}

	private ModuleManager	moduleManager;
	private ActionBean			actionBean;
	private ActionConfig	parent;
	private ForwardTarget	parentName;

	private ActionConfig(ActionBean actionBean, ModuleManager moduleManager)
	{
		this.actionBean = actionBean;
		this.moduleManager = moduleManager;
		if (actionBean.getParent() != null)
			parentName = new ForwardTarget(actionBean.getParent());
	}

	ActionBean getActionBean()
	{
		return actionBean;
	}

	public String getName()
	{
		return actionBean.getName();
	}

	public boolean isAbstract()
	{
		return actionBean.isAbstract();
	}

	public ActionConfig getParentConfig() throws ActionConfigInvalidException
	{
		if (null == parentName)
			return null;
		if (null == parent)
		{
			try
			{
				Module m = moduleManager.getModule(parentName.getModuleName());
				parent = m.getActionManager().getActionConfig(parentName.getActionName());
			}
			catch (ModuleNotFoundException e)
			{
				throw new ActionConfigInvalidException(e.getMessage());
			}
		}
		return parent;
	}

	String getFullName()
	{
		StringBuffer sb = new StringBuffer();
		sb.append(this.getName());
		try
		{
			ActionConfig p = this.getParentConfig();
			if (null != p)
				sb.append(" > ").append(p.getFullName());
		}
		catch (ActionConfigInvalidException e)
		{
			sb.append(" > ").append(e.getMessage());
		}
		return sb.toString();
	}

	public String getComment()
	{
		return actionBean.getComment();
	}

	public String getActionClassName()
	{
		return actionBean.getType();
	}

	public int size_property() throws ActionConfigInvalidException
	{
		Map map = actionBean.getProperties();
		if (null == map)
			return 0;
		return map.size();
	}

	public int size_param() throws ActionConfigInvalidException
	{
		Map map = actionBean.getParams();
		if (null == map)
			return 0;
		return map.size();
	}

	public ActionProperty getProperty(String name) throws ActionConfigInvalidException
	{
		Map<String, PropertyBean> map = actionBean.getProperties();
		if (null == map)
			return null;
		PropertyBean bean = map.get(name);
		if (null == bean)
			return null;
		return ActionProperty.wrap(bean);
	}

	public String getPropertyValue(String name)
	{
		ActionProperty ap;
		try
		{
			ap = getProperty(name);
		}
		catch (Exception e)
		{
			return null;
		}
		if (null == ap)
			return null;
		return ap.getValue();
	}

	public String getPropertyValue(String name, String def)
	{
		String v = this.getPropertyValue(name);
		if (null == v)
			return def;
		return v;
	}

	public Iterator<ParamBean> paramIterator()
	{
		if (null == actionBean.getParams())
			return null;
		return actionBean.getParams().values().iterator();
	}

	public Iterator<PropertyBean> propertyIterator()
	{
		if (null == actionBean.getProperties())
			return null;
		return actionBean.getProperties().values().iterator();
	}

	public ParamBean getParam(String name)
	{
		Map<String, ParamBean> map = actionBean.getParams();
		if (null == map)
			return null;
		return map.get(name);
	}
}
